//
//  MoreNewsView.m
//  MoXiProject
//
//  Created by 沈晨宇 on 2017/7/10.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "MoreNewsView.h"
#import "HomeViewModel.h"
#import "MainNewsModel.h"
#import "NewsTableViewCell.h"
#import "NewsDetailsViewController.h"
@interface MoreNewsView ()<UITableViewDelegate,UITableViewDataSource>
{
    UIImageView * commentImageView;
    NSString* typeId;
}
@property(nonatomic,strong)UITableView* moreNews;
@property(nonatomic,strong)HomeViewModel * homeViewModel;
@property (nonatomic,strong) UIView * tempView;
@end

@implementation MoreNewsView

-(HomeViewModel *) oneViewModel{
    if (!_homeViewModel) {
        _homeViewModel = [[HomeViewModel alloc] init];
    }
    return _homeViewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self createTempView];
    typeId=@"1";
    
}

-(void)createTempView{
    
    self.tempView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,self.moreNews.width, self.moreNews.height)];
    
    
    
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(DEF_DEVICE_WIDTH/2-25, 150, 50, 50)];
    imageview.image = [UIImage imageNamed:@"wushuju"];
    imageview.contentMode = UIViewContentModeScaleAspectFit;
    [self.tempView addSubview:imageview];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,200, DEF_DEVICE_WIDTH, 30)];
    label.textColor = DEF_COLOR_RGB(215, 215, 215);
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:15.f];
    label.text = @"暂无数据，我们会尽快添加";
    [self.tempView addSubview:label];
    
    [self.moreNews addSubview:self.tempView];
    self.tempView.hidden = YES;
}

-(void)initUI{
    
    self.moreNews=[UITableView new];
    self.moreNews.delegate=self;
    self.moreNews.dataSource=self;
    self.moreNews.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.moreNews];
   if(iPhoneX){
        
        [self.moreNews mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(DEF_DEVICE_WIDTH);
            make.height.mas_equalTo(DEF_DEVICE_HEIGHT-44-64-49-50-10);
            make.top.equalTo(self.view).with.offset(0);
            make.leading.equalTo(self.view).with.offset(0);
        }];
        
    }else{
        [self.moreNews mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(DEF_DEVICE_WIDTH);
            make.height.mas_equalTo(DEF_DEVICE_HEIGHT-44-64-49);
            make.top.equalTo(self.view).with.offset(0);
            make.leading.equalTo(self.view).with.offset(0);
        }];
        
    }
    
}

- (void)zj_viewDidLoadForIndex:(NSInteger)index {
    
    switch (_index) {
        case 0:
            
            typeId=@"1";
            
            break;
        case 1:
            
            typeId=@"2";
            
            break;
        case 2:
            
            typeId=@"3";
            
            break;
        case 3:
            
            typeId=@"4";
            
            break;
            
        default:
            break;
    }
    [MXUtility showMBProgress:self.view message:LoadingText];
    
    [[self.homeViewModel mainNewsRequestpParameter:@"0" pageSize:@"50" typeId:typeId] subscribeNext:^(id result) {
        
        if([result[@"errorcode"] integerValue] == 0){
            
            if (self.oneViewModel.homeArrayList.count==0) {
                
                self.tempView.hidden = NO;
                
            }else{
                
                self.tempView.hidden = YES;
            }
            
            [self.moreNews reloadData];
            
        }else{
            
            [MXUtility showTips:@"获取新闻列表失败"];
            
        }
        
        [MXUtility hideMBProgress:self.view];
        
    }error:^(NSError *error) {
        
        [MXUtility hideMBProgress:self.view];
        
    }];
    
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.oneViewModel.homeArrayList.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [self tableView:tableView cellForRowAtIndexPath:indexPath].frame.size.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"mainNewsCell";
    NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        
        cell = [[NewsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.model=self.oneViewModel.homeArrayList[indexPath.row];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MainNewsModel* model=self.oneViewModel.homeArrayList[indexPath.row];
    //    新闻详情页
    NewsDetailsViewController* vc=[[NewsDetailsViewController alloc]init];
    vc.hidesBottomBarWhenPushed=YES;
    vc.newsId=model.newsId;
    vc.controllerFromWhere=UIViewControllerFromOther;
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    CATransform3D transform3D = CATransform3DMakeRotation(M_PI_2, 0.0, 1.0, 1.0);
    cell.alpha = 0.0;
    cell.layer.transform = transform3D;
    cell.layer.anchorPoint = CGPointMake(0.0, 0.5); // 设置锚点位置；默认为中心点(0.5, 0.5)
    
    [UIView beginAnimations:@"transform" context:NULL];
    [UIView setAnimationDuration:0.5];
    cell.alpha = 1.0;
    cell.layer.transform = CATransform3DIdentity;
    CGRect rect = cell.frame;
    rect.origin.x = 0.0;
    cell.frame = rect;
    [UIView commitAnimations];
}
@end
